package com.operation;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.hibernate.annotations.ParamDef;
import org.json.JSONObject;

import com.enetiy.Binding;
import com.mysql.jdbc.Util;
import com.radiusmess.MD5;
import com.radiusmess.RadiusInformation;
import com.radiusmess.RadiusParser;
import com.radiusmess.RadiusTester;
import com.service.BindingService;
import com.util.HexUtil;
import com.util.Log4gUtil;
import com.util.RandomUtil;
import com.util.UDP;

public class HotspotLink {
	
	private static ResourceBundle bundle = PropertyResourceBundle.getBundle("com.config.verify");
	private static String ACControl = bundle.getString("ACControl");
	public String name;
	public String type;
	public String msg;
	
	/**
	 * 绑定验证 
	 * @param account
	 * @param accountPwd
	 * @param areaid
	 * @param url
	 * @param gateIp
	 * @param userid
	 * @param NF_callid
	 * @param token
	 * @return
	 */
	public  int  hotspotVerify(String account,String accountPwd,int areaid,String url,String gateIp,int userid,String NF_callid, String token){
		long startTime = new Date().getTime();
		HttpSendUrl send = new HttpSendUrl();
		String param = "verify_code="+account+"&id_card="+accountPwd+"&protectid="+areaid;
		url="http://"+gateIp+url;
		Log4gUtil.Info("hotspotVerify-绑定", userid+"", "url", url+"?"+param, NF_callid, token);
		
		String r ="0";
		String code="";
		ACControl = "true";
		if (!ACControl.equals("false")){
			try {
				r = send.getSendRequest(url, param, "POST",5000);
				JSONObject jn = new JSONObject(r);
				if(jn.has("code"))
					code = jn.get("code").toString();
				if(jn.has("name"))
					name = jn.get("name").toString();
				if(jn.has("type"))
					type = jn.get("type").toString();
				if(jn.has("msg"))
					msg = jn.get("msg").toString();
			} catch (Exception e) {
				code="-1";
			}
		}else{
			code="1";
		}
		Long endTime = new Date().getTime();
		Log4gUtil.Info("hotspotVerify-绑定结果", userid+"", "绑定时间与结果", (startTime-endTime)+"-"+url+"?"+param+"-"+r, NF_callid, token);
		return Integer.parseInt(code);
	}
	
	
	
	/*public static void main(String[] args) {
		hotspotVerify("10003","12345678",2,"172.16.30.213:8080/protal/toSZ/checkuser.do","");
	}*/
	/*//热点验证
	public static int  hotspotVerify(String account,String accountPwd,String url) throws Exception{
		int co = 0;
		if(!BindingControl.equals("false")){
			RadiusInformation ri =new RadiusInformation();
			ri.setUserName(account);
			ri.setPassword(accountPwd);
			ri.setRadiusKey(radiusKey);
			RadiusTester r= new RadiusTester(ri);
			r.makePackage(true, false);
			
			int l =  UDP.setSocket(radiusIP,r.getAuthContent(), r.getAuthContent().length, 1812);
			byte[] socket = UDP.getSocket(l);
			//解析报文
			RadiusParser rp = new RadiusParser();
			
			rp.getAuthVerify(socket, r, ri);
			String hexVerofy = HexUtil.chang(rp.getAuth_verify());//验证返回报文验证字(预生成)
			String hexMess = HexUtil.chang(rp.getMess());		// 返回报文
			
			String success = "120A7375636365737321";//正确结果
			String verifgmess = hexMess.substring(8, 40);	//返回报文中的验证字
			String  result = hexMess.substring(40, 60);		//返回报文中的结果
			
			if(hexVerofy.equals(verifgmess)&&result.equals(success)){
				co=1;
			}
		}else{
			co=1;
		}
		return co;
	}*/
	
/*	//热点登陆
	public static  int hotspotLogin(String account,String accountPwd,int id,String url) throws Exception{
		int co = 0;
		if(!ACControl.equals("false")){
			RadiusInformation ri =new RadiusInformation();
			ri.setUserName(account);
			ri.setPassword(accountPwd);
			ri.setRadiusKey(radiusKey);  
			ri.setSessId(id);
			
			RadiusTester r= new RadiusTester(ri);
			r.makePackage(false, true);
			
			int setSocket = UDP.setSocket(radiusIP,r.getAcctContent(), r.getAcctContent().length, 1813);
			byte[] socket = UDP.getSocket(setSocket);
			
			RadiusParser rp = new RadiusParser();
			//解析报文
			rp.getAcctVerify(socket, r, ri);
			String verifymess = HexUtil.chang(rp.getAcct_verify());
			String hexMess = HexUtil.chang(rp.getMess());
			System.out.println("上线返回报文验证字-------"+verifymess);
			System.out.println("上线返回报文-------"+ hexMess);
			String substring = hexMess.substring(8, 40);
			System.out.println("截取报文中验证字---------"+substring);
			
			if(substring.equals(verifymess)){
				co=1;
			}
		}else{
			co=1;
		}
		return co;
	}
	
	//退出热点登陆
	public static  int hotspotOutlogin(String account,String accountPwd ,int id,String url) throws Exception{
		int co = 0;
		if(!BindingControl.equals("false")){
		
			RadiusInformation ri =new RadiusInformation();
			ri.setUserName(account);
			ri.setPassword(accountPwd);
			ri.setRadiusKey(radiusKey);
			ri.setSessId(id);
			
			RadiusTester r= new RadiusTester(ri);
			r.makePackage(false, false);

			int setSocket = UDP.setSocket(radiusIP,r.getAcctContent(), r.getAcctContent().length, 1813);
			byte[] socket = UDP.getSocket(setSocket);
			HexUtil.chang(socket);
			RadiusParser rp = new RadiusParser();
			
			//解析报文
			rp.getAcctVerify(socket, r, ri);
			String verifymess = HexUtil.chang(rp.getAcct_verify());
			String hexMess = HexUtil.chang(rp.getMess());
			System.out.println("下线返回报文验证字-------"+verifymess);
			System.out.println("下线返回报文-------"+ hexMess);
			String substring = hexMess.substring(8, 40);
			System.out.println("截取报文中验证字---------"+substring);
			
			if(substring.equals(verifymess)){
				co=1;
			}
			
		}else{
			co= 1;
		}
		return co;
	}
*/
}
